# -*- encoding: utf-8 -*-
import time

from odoo import models, fields, api
from odoo.addons import base_cw


class MoneyOrder(models.Model):
    _name = 'money.order'
    _description = '汇票'
    _rec_name = 'bill_number'

    """收到日期、付款凭证号、票据号码（汇票编号）、出票人全称、背书单位、出票银行、汇票金额、出票日期、票据到期日、汇款到期天数、领用人、日   期、金额、转让凭证号"""
    bill_number = fields.Char(string='票据号码')
    bill_date = fields.Date(string='出票日期')
    bill_due_date = fields.Date(string='票据到期日')
    overdue_days = fields.Integer(string='汇票到期天数', compute='_compute_is_use')
    currency_id = fields.Many2one(comodel_name='res.currency', string='币别',
                                  default=lambda self: self.env.user.company_id.currency_id)
    money_order_money = fields.Monetary(string='汇票金额', currency_field='currency_id')
    # todo 如何添加汇票使用标记
    is_use = fields.Boolean(string='可使用', default=True, help="勾选为可使用", compute='_compute_is_use',
                            store=True)
    money_order_line_ids = fields.One2many(string="汇票明细", comodel_name='money.order.line',
                                           inverse_name='money_order_id')

    @api.depends('money_order_line_ids', 'money_order_line_ids.payment_id', 'bill_due_date', 'overdue_days', 'is_use')
    def _compute_is_use(self):
        for record in self:
            is_use = True
            record.overdue_days = 0
            if record.bill_due_date:
                record.overdue_days = base_cw.public.get_days_between_date(time.strftime("%Y-%m-%d"),
                                                                           fields.Datetime.to_string(
                                                                               record.bill_due_date))
                if not record.overdue_days > 0:
                    is_use = False
            if len(record.money_order_line_ids) > 0:
                payment_ids = record.money_order_line_ids.filtered(lambda e: e.payment_id != False)
                if len(payment_ids) > 0:
                    is_use = False
            record.is_use = is_use


class MoneyOrderLine(models.Model):
    _name = 'money.order.line'
    _description = '汇票明细行'

    money_order_id = fields.Many2one(comodel_name='money.order', string='关联汇票')
    company_id = fields.Many2one(comodel_name='res.company', string='所属公司', default=lambda self: self.env.company)
    currency_id = fields.Many2one(comodel_name='res.currency', string='币别',
                                  default=lambda self: self.env.user.company_id.currency_id)

    drawer = fields.Many2one(comodel_name='res.partner', string='出票人')
    transfer_partner_id = fields.Many2one(comodel_name='res.partner', string='背书单位')
    partner_bank_id = fields.Many2one(comodel_name='res.partner.bank', string='出票银行')
    payee = fields.Many2one(comodel_name='res.partner', string='付款人', defualt=lambda self: self.env.company.id)
    transfer_money = fields.Monetary(string='金额', currency_field='currency_id')

    payment_date = fields.Date(string='付款日期')
    payment_id = fields.Many2one(comodel_name='account.pay', string='付款单', ondelete='cascade')
    payment_line_id = fields.Many2one(comodel_name='account.pay.line', string='付款单明细', ondelete='cascade')
    payment_voucher_id = fields.Many2one(comodel_name='account.voucher', string='付款凭证', ondelete='cascade')

    receive_date = fields.Date(string='收款日期')
    receive_id = fields.Many2one(comodel_name='account.receive', string='收款单', ondelete='cascade')
    receive_line_id = fields.Many2one(comodel_name='account.receive.line', string='收款单明细', ondelete='cascade')
    received_voucher_id = fields.Many2one(comodel_name='account.voucher', string='收款凭证', ondelete='cascade')
    reversal_voucher_id = fields.Many2one(comodel_name='account.voucher', string='冲销凭证', ondelete='cascade')
